/// Created by Nazariy Jaworski Copyright (c) 2013
/// Bot program
///
/// botui.h -
///  implements user interface (web WT) for
///  controlling application behavior
///
/// To Do list:
///  - ...
///
/// v 0.1        08.04.2013
#ifndef BOTWTUI_H
#define BOTWTUI_H

//it for usage of Qt native macro SIGNAL() and SLOT()
#define WT_NO_SLOT_MACROS

#include <stdlib.h>
#include <QThread>
#include <QString>
#include <Wt/WServer>
#include <Wt/WApplication>
#include <Wt/WVBoxLayout>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WTextArea>

#include "botwtglwidget.h"

class BotUI;    //just forward declaration

using namespace Wt;
/// \todo make connections

/// WT Web ToolKit User Interface
/// For now it is simple web copy of BotUI console interface
class BotWtUI : public QObject, public WApplication//, public BotUICommands
{
    Q_OBJECT

    private: WVBoxLayout    *_myLayout      = nullptr;
    private: BotWtGLWidget  *_GLWidget      = nullptr;
    private: WLineEdit      *_commandEdit   = nullptr;
    private: WTextArea      *_logTextArea   = nullptr;
    //private: QString        _hiddenLogText;   // I have already done it as global static at *.cpp
    public:  QString        lastCommand;

    //public: static std::vector<WApplication*> currentSessions; // see comment above

    Q_SIGNAL void SIGNAL_RegisterAtEnvironment() const;
    ///< UI Command - use to register Bot at environment

    Q_SIGNAL void SIGNAL_ShowClients() const;
    ///< UI Command - use to show connected clients

    Q_SIGNAL void SIGNAL_Exit() const;
    ///< UI Command - use to exit application

    Q_SIGNAL void SIGNAL_ShowOCLPlatforms() const;
    ///< UI Command - use to show OpenCL platforms

    Q_SIGNAL void SIGNAL_ShowOCLDevices() const;
    ///< UI Command - use to show OpenCL platforms

    Q_SIGNAL void SIGNAL_FatalError(QString message) const;
    ///< UI Output, connect to Bot SLOT_FatalError, use to throw exeption and exit application

    Q_SIGNAL void SIGNAL_WriteToLog(const QString message)  const;
    ///< Signal for writing data to log file (do not use for now, Logger automatically does it)

    /// Web UI redirect to console Ui
    Q_SIGNAL void SIGNAL_WriteConsoleUIString(QString message) const;

    /// UI Input - writes simple string
    public: Q_SLOT void SLOT_WriteUIString(QString message);

    /// UI Input - writes simple string
    public: Q_SLOT void SLOT_WriteUIStringFromConsole(QString message);

    /// UI Input - makes user Ui update by Server
    public: Q_SLOT void SLOT_UpdateFromServer();

    /// Slot for closing web Interface
    public: Q_SLOT void SLOT_Close() { quit();} ///< \todo

    private: void _UICommandExecutor();

    /// do not forget to init _refToParent
    public:	BotWtUI(const WEnvironment &env);

    public: static void updateAllUserSessions();

    public: ~BotWtUI();
};

class WebServerRunner : public QThread
{
    public: WServer *_myWServer = nullptr;
    private: static WApplication *createApplication(const WEnvironment& env);
    public: WebServerRunner(){}
    /// \todo avoid std stream out
    public: void run();

    public: ~WebServerRunner()
    {
        if(_myWServer)
            delete _myWServer;
    }
};

#endif // BOTWTUI_H
